<?php

/**
 * @name /skm_www/Tag.php
 * @author Kimberly Berger <knb4850@esu.edu>
 * @version
 *
 * This is the Tag page.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
ReqLib('/lib/private/Item.php');
ReqLib('/lib/private/ItemSearchWidget.php');

function DefineHeader() {
?><script type="text/javascript">
	$(function() {
		$('#TAGS').dataTable({
			'bJQueryUI': true,
			'sPaginationType': 'full_numbers',
			asSortingof: false
		});
		function onModifyTags(select, value, text) {
			$tags = $('#TagSelect Option');
			var text = '';
			$.each($tags, function(i, tag) {
				text += (i ? '|' : '') + tag.value;
				});
			$('#TagHidden').val(text);
		}
			$('TAGS').dataTable({
				bJQueryUI: true,
				bSort: false,
				bFilter: false,
				bPaginate: false,
				bInfo: false,
			});
		$('input[type=button], input[type=submit]').button();
	});
	$(function() {
		$('#TagSelect').selectList({
			onAdd: onModifyTags,
			onRemove: onModifyTags
		});
	});

 </script>
<?
}
function DefinePreload(){
  $user = new User();
  $user->Load($_SESSION['userId']);
  if (!$user->UserId) {
    header('location: login_register.php?pagereq=Item.php');
	die();
	}
  if(!$user->HasRole('Administrator')){
  	header('location: login_register.php?pagereq=Item.php');
	die();
	}

	$i = new Item();
	if(isset($_POST['Update'])){

		if(!$i->Load($i->GetItemIdByItemName($_POST['ItemName']))){
			$_SESSION['Message'] = 'This item is unable to be loaded or does not exist.';
			header('location: Tag.php');
			die();
		}

		$Tags = $_POST['Tags'];
		$Tags = explode("|", $Tags);

		$i->UntagAllTags();

		foreach($Tags as $tag){
			$i->TagItem($tag);
		}
		if($i->save())
			$_SESSION['Message'] = 'The tags have been added to the item';
		else{
			$_SESSION['Message'] = 'The tags have not been added to the item';
		}
	}
}
/**
 * Defines page content.
 *
 */
function DefineContent() {
  // Page content goes here.
  ?>
<h1>Tag Modification</h1>
<p>This is the Tag Modification Page</p>
<p>
  <a href="<?=_webroot('/') ?>">Home</a>
</p>
<?
	if($_SESSION['userId'] >0){
?>
		<p>
		  <a href="<?=_webroot('/account.php') ?>">My Account</a>
		</p>
		<p>
		  <a href="<?=_webroot('/logout.php') ?>">Logout</a>
		</p>
<? }
   else{
?>
		<p>
		  <a href="<?=_webroot('/login_register.php') ?>">Login/Register</a>
		</p>
<?
   }
?>
  <hr />
  <center>
<?
	if(isset($_SESSION['Message'])){
		print ($_SESSION['Message'] . "<br / >");
		unset($_SESSION['Message']);
	}
?>
	<a href="<?=_webroot('/Tag.php?Add')?>">Add Tag</a>
<p>
</p>
<?	if(isset($_POST['Update'])){?>
		<form method="POST" action="Tag.php?AddedTag">
			<table class="display" border="1" id="TAGS" width="400">
				<tbody>
					<tr>
						<td> Tag Name:</td>
						<td><input type="text" name="NewTag" /></td>
					</tr>
				</tbody>
			</table>
		</form>
<?	}

	$item = new Item();
?>
	<form method="POST" action="Tag.php?Update">
		<table class="display" border="1" id="TAGS" width="400">
			<tbody>
				<tr>
					<td>Item Name</td>
					<td><input type="text" name="ItemName"/></td>
				</tr>
				<tr>
					<td>Tags</td>
					<td>
					<?
						$tagOptions = Item::GetTags();
						array_push($tagOptions, 'Select Tags...');
						for($i = 0; $i < count($tagOptions); $i++){
							$o = $tagOptions[$i];
							$o = '<option value="' . htmlspecialchars($o) . '"' . ($item->HasTag($o) ? ' selected="selected"' : ''). '>' . htmlspecialchars($o) . '</option>';
							$tagOptions[$i] = $o;
						}
						$tagOptions = implode('', $tagOptions);
						print('<select id="TagSelect" multiple="multiple" title="Select Tags...">' . $tagOptions . '</select>');
						print('<input type="hidden" name="Tags" id="TagHidden" value="' . implode('|', $item->_Tags) . '" />');
					?>
					</td>
				</tr>
				<tr>
					<td align="right"><input type="submit" name="Update" value="Update Item Tags" /></td>
					<td></td>
				</tr>
			</tbody>
		</table>
	</form>
</center>
<?
}
// Launches the page; without this, nothing happens.
require_once('_loader.php');